<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="主机名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="主机名称"></el-input>
    </el-form-item>
    <el-form-item label="设备类型" prop="deviceType">
      <el-input v-model="dataForm.deviceType" placeholder="设备类型"></el-input>
    </el-form-item>
    <el-form-item label="所属租户" prop="lessee">
      <el-input v-model="dataForm.lessee" placeholder="所属租户"></el-input>
    </el-form-item>
    <el-form-item label="序列号" prop="serialNo">
      <el-input v-model="dataForm.serialNo" placeholder="序列号"></el-input>
    </el-form-item>
    <el-form-item label="通讯IP地址" prop="ipLocal">
      <el-input v-model="dataForm.ipLocal" placeholder="通讯IP地址"></el-input>
    </el-form-item>
    <el-form-item label="通讯端口" prop="portLocal">
      <el-input v-model="dataForm.portLocal" placeholder="通讯端口"></el-input>
    </el-form-item>
    <el-form-item label="设备IP" prop="ip">
      <el-input v-model="dataForm.ip" placeholder="设备IP"></el-input>
    </el-form-item>
    <el-form-item label="设备端口" prop="port">
      <el-input v-model="dataForm.port" placeholder="设备端口"></el-input>
    </el-form-item>
    <el-form-item label="设备数" prop="deviceCount">
      <el-input v-model="dataForm.deviceCount" placeholder="设备数"></el-input>
    </el-form-item>
    <!-- <el-form-item label="创建时间" prop="createdAt">
      <el-input v-model="dataForm.createdAt" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updatedAt">
      <el-input v-model="dataForm.updatedAt" placeholder="修改时间"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="" prop="username">
      <el-input v-model="dataForm.username" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="password">
      <el-input v-model="dataForm.password" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="streamMonitorFlag">
      <el-input v-model="dataForm.streamMonitorFlag" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="f4gFlag">
      <el-input v-model="dataForm.f4gFlag" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="applianceMode">
      <el-input v-model="dataForm.applianceMode" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="applianceUptime">
      <el-input v-model="dataForm.applianceUptime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="serviceUptime">
      <el-input v-model="dataForm.serviceUptime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="systemTime">
      <el-input v-model="dataForm.systemTime" placeholder=""></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          deviceType: '',
          lessee: '',
          serialNo: '',
          ipLocal: '',
          portLocal: '',
          ip: '',
          port: '',
          deviceCount: '',
          createdAt: '',
          updatedAt: '',
          username: '',
          password: '',
          streamMonitorFlag: '',
          f4gFlag: '',
          applianceMode: '',
          applianceUptime: '',
          serviceUptime: '',
          systemTime: ''
        },
        dataRule: {
          name: [
            { required: true, message: '主机名称不能为空', trigger: 'blur' }
          ],
          deviceType: [
            { required: true, message: '设备类型不能为空', trigger: 'blur' }
          ],
          lessee: [
            { required: true, message: '所属租户不能为空', trigger: 'blur' }
          ],
          serialNo: [
            { required: true, message: '序列号不能为空', trigger: 'blur' }
          ],
          ipLocal: [
            { required: true, message: '通讯IP地址不能为空', trigger: 'blur' }
          ],
          portLocal: [
            { required: true, message: '通讯端口不能为空', trigger: 'blur' }
          ],
          ip: [
            { required: true, message: '设备IP不能为空', trigger: 'blur' }
          ],
          port: [
            { required: true, message: '设备端口不能为空', trigger: 'blur' }
          ],
          deviceCount: [
            { required: true, message: '设备数不能为空', trigger: 'blur' }
          ],
          createdAt: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updatedAt: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          streamMonitorFlag: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          f4gFlag: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          applianceMode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          applianceUptime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          serviceUptime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          systemTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/product/host/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.host.name
                this.dataForm.deviceType = data.host.deviceType
                this.dataForm.lessee = data.host.lessee
                this.dataForm.serialNo = data.host.serialNo
                this.dataForm.ipLocal = data.host.ipLocal
                this.dataForm.portLocal = data.host.portLocal
                this.dataForm.ip = data.host.ip
                this.dataForm.port = data.host.port
                this.dataForm.deviceCount = data.host.deviceCount
                this.dataForm.createdAt = data.host.createdAt
                this.dataForm.updatedAt = data.host.updatedAt
                this.dataForm.username = data.host.username
                this.dataForm.password = data.host.password
                this.dataForm.streamMonitorFlag = data.host.streamMonitorFlag
                this.dataForm.f4gFlag = data.host.f4gFlag
                this.dataForm.applianceMode = data.host.applianceMode
                this.dataForm.applianceUptime = data.host.applianceUptime
                this.dataForm.serviceUptime = data.host.serviceUptime
                this.dataForm.systemTime = data.host.systemTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/product/host/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'deviceType': this.dataForm.deviceType,
                'lessee': this.dataForm.lessee,
                'serialNo': this.dataForm.serialNo,
                'ipLocal': this.dataForm.ipLocal,
                'portLocal': this.dataForm.portLocal,
                'ip': this.dataForm.ip,
                'port': this.dataForm.port,
                'deviceCount': this.dataForm.deviceCount,
                'createdAt': this.dataForm.createdAt,
                'updatedAt': this.dataForm.updatedAt,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'streamMonitorFlag': this.dataForm.streamMonitorFlag,
                'f4gFlag': this.dataForm.f4gFlag,
                'applianceMode': this.dataForm.applianceMode,
                'applianceUptime': this.dataForm.applianceUptime,
                'serviceUptime': this.dataForm.serviceUptime,
                'systemTime': this.dataForm.systemTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
